/**
 * Project: ld.biz.web
 * 
 * File Created at 2012-9-23
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.liangdev.web.action.autocode;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.liangdev.biz.core.autocode.DBMetaDataManager;
import com.liangdev.biz.dataobject.autocode.DbInfoDO;
import com.liangdev.biz.entity.autocode.TableMetaData;
import com.liangdev.biz.service.autocode.DbInfoService;
import com.liangdev.biz.web.util.ActionUtil;
import com.liangdev.common.util.StringUtil;
import com.liangdev.web.vo.TreeNode;
import com.liangdev.web.vo.TreeNodeHasChecked;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 获取数据库配置信息
 * 
 * @author chongan.wangca
 */
public class DbInfoAction extends ActionSupport {

    private static final long serialVersionUID = -5147166361020554487L;

    private final Logger      logger           = LoggerFactory.getLogger(DbInfoAction.class);

    @Resource
    private ActionUtil        actionUtil;

    @Resource
    private DbInfoService     dbInfoService;

    @Resource
    private DBMetaDataManager dbMetaDataManager;

    public void getDbInfoJsonByDbType() {
        String id = actionUtil.getRequestParamString("id");
        String dbType = actionUtil.getRequestParamString("dbType");
        if (StringUtil.isEquals(id, "root")) {
            List<DbInfoDO> dbInfoDOList = dbInfoService.getByDbType(dbType);
            try {
                String json = makeDbInfoJson(dbInfoDOList);
                actionUtil.getResponse().getWriter().print(json);
            } catch (IOException e) {
                logger.error("", e);
            }
        } else {
            try {
                List<TableMetaData> tableMetaDatas = dbMetaDataManager.getAllTables(Long
                        .valueOf(id));
                try {
                    String json = makeTableInfoJson(id, tableMetaDatas);
                    actionUtil.getResponse().getWriter().print(json);
                } catch (IOException e) {
                    logger.error("", e);
                }
            } catch (Exception e) {
                logger.error("", e);
                //                try {
                //                    actionUtil.getResponse().getWriter().print("error|" + e.getMessage());
                //                } catch (IOException e1) {
                //                    logger.error("", e1);
                //                }
            }
            return;
        }
    }

    /**
     * 转化为JSON
     * 
     * @param dbInfoDOList
     * @return
     */
    private String makeDbInfoJson(List<DbInfoDO> dbInfoDOList) {

        if (dbInfoDOList == null || dbInfoDOList.size() == 0) {
            return null;
        }

        List<TreeNode> treeNodes = new ArrayList<TreeNode>();
        for (DbInfoDO dbInfoDO : dbInfoDOList) {
            TreeNode treeNode = new TreeNode();
            treeNode.setId(dbInfoDO.getId() + "");
            treeNode.setCls("folder");
            treeNode.setLeaf(false);
            treeNode.setIconCls("database-button");
            treeNode.setText(dbInfoDO.getDbNameCn() + "(" + dbInfoDO.getDbName() + ")");
            treeNodes.add(treeNode);
        }
        JSONArray ja = JSONArray.fromObject(treeNodes);
        //System.out.println(ja.toString());
        return ja.toString();
    }

    /**
     * 转化为JSON
     * 
     * @param dbInfoDOList
     * @return
     */
    private String makeTableInfoJson(String dbInfoId, List<TableMetaData> tableMetaDataList) {

        if (tableMetaDataList == null || tableMetaDataList.size() == 0) {
            return null;
        }

        List<TreeNodeHasChecked> treeNodes = new ArrayList<TreeNodeHasChecked>();
        for (TableMetaData tableMetaData : tableMetaDataList) {
            TreeNodeHasChecked treeNode = new TreeNodeHasChecked();
            //叶子结点的ID格式：数据库ID+表名
            treeNode.setId(dbInfoId + "|" + tableMetaData.getDbTableName().toUpperCase());
            treeNode.setLeaf(true);
            treeNode.setIconCls("table-button");
            treeNode.setText(tableMetaData.getDbTableName().toUpperCase());
            treeNode.setQtip(tableMetaData.getDbTableName().toUpperCase());
            treeNodes.add(treeNode);
        }
        JSONArray ja = JSONArray.fromObject(treeNodes);
        //System.out.println(ja.toString());
        return ja.toString();
    }
}
